package com.aiju.cash.ui.signbank.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.aiju.base.activity.BaseTitleActivity;
import com.aiju.cash.R;
import com.aiju.cash.ui.enter.util.EnterHelper;
import com.aiju.cash.ui.signbank.bean.BusinessLicenseInfo;
import com.aiju.cash.util.ImageDialogUtil;
import com.aiju.forexpand.datepicker.DatePicker;
import com.aiju.utils.DateUtil;
import com.aiju.utils.L;
import com.aiju.utils.StringUtil;
import com.aiju.utils.T;
import com.aiju.widgit.layout.SettingItemEditView;
import com.aiju.widgit.layout.SettingItemRadioButton;
import com.aiju.widgit.layout.SettingItemTextView;

import java.io.File;

/**
 * @author: 流冥
 * @date: 2017-07-13
 * @Description: 营业执照信息提交
 */
public class BusinessLicenseInfoActivity extends BaseTitleActivity implements View.OnClickListener {
    private SettingItemRadioButton license;
    private SettingItemTextView effect_time;
    private SettingItemTextView expire_time;
    private SettingItemEditView code;
    private BusinessLicenseInfo businessLicenseInfo = new BusinessLicenseInfo();
    private ImageView imageView;
    private File logoFile;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_business_license_info);
        initView();
    }

    void initView() {
        setCenterTitle("营业执照信息提交");
        setRightTitle("提交");
        license = (SettingItemRadioButton) findViewById(R.id.license);
        effect_time = (SettingItemTextView) findViewById(R.id.effect_time);
        expire_time = (SettingItemTextView) findViewById(R.id.expire_time);
        code = (SettingItemEditView) findViewById(R.id.code);
        imageView = (ImageView) findViewById(R.id.image);
        license.setRadio1Text("三证合一");
        license.setRadio2Text("非三证合一");
        code.setEditable(true);
        EditText rightView = (EditText) code.getRightView();
        rightView.setHint("请填写");
        rightView.setHintTextColor(Color.parseColor("#bbbbbb"));
        rightView.setCursorVisible(false);
        rightView.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        effect_time.setOnClickListener(this);
        expire_time.setOnClickListener(this);

        mAiJuTitleBarView.getRightTextView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (checkInfo()) {
                    // TODO: 2017/7/13 提交信息
                    L.i("AiJu",businessLicenseInfo.toString());
                }
            }
        });
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ImageDialogUtil.showSelectPicDialog(BusinessLicenseInfoActivity.this);
            }
        });
        license.setChangeListener(new SettingItemRadioButton.OnChangeListener() {
            @Override
            public void change(String item) {
                businessLicenseInfo.setLicenseType(item);
            }
        });

        code.getRightView().addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                businessLicenseInfo.setCode(s.toString().toString().trim());
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.effect_time:
                showEffectTime(effect_time.getRightView(), "营业执照生效时间");
                break;
            case R.id.expire_time:
                showExpireTime(expire_time.getRightView(), "营业执照失效时间");
                break;
        }
    }


    private boolean checkInfo() {
        if (StringUtil.isBlank(businessLicenseInfo.getLicenseType())) {
            T.ToastShort("请选择营业执照类型");
            return false;
        }

        if (StringUtil.isBlank(businessLicenseInfo.getEffectTime())) {
            T.ToastShort("请选择营业执照生效时间");
            return false;
        }

        if (StringUtil.isBlank(businessLicenseInfo.getExpireTime())) {
            T.ToastShort("请选择营业执照过期时间");
            return false;
        }

        if (StringUtil.isBlank(businessLicenseInfo.getCode())) {
            T.ToastShort("请选择营业执照编号");
            return false;
        }
        if (logoFile == null) {
            T.ToastShort("请上传营业执照图片");
            return false;
        }

        return true;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        logoFile = EnterHelper.onActivityResult(imageView, requestCode, resultCode, data);
    }

//    /**
//     * 获取圆角位图的方法
//     *
//     * @param bitmap
//     *            需要转化成圆角的位图
//     * @param pixels
//     *            圆角的度数，数值越大，圆角越大
//     * @return 处理后的圆角位图
//     */
//    public static Bitmap toRoundCornerImage(Bitmap bitmap, int pixels) {
//        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
//        Canvas canvas = new Canvas(output);
//        final int color = 0xff424242;
//        final Paint paint = new Paint();
//        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
//        final RectF rectF = new RectF(rect);
//        final float roundPx = pixels;
//        // 抗锯齿
//        paint.setAntiAlias(true);
//        canvas.drawARGB(0, 0, 0, 0);
//        paint.setColor(color);
//        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
//        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
//        canvas.drawBitmap(bitmap, rect, rect, paint);
//        return output;
//    }


    /**
     * 生效时间
     */
    private void showEffectTime(TextView textView, String title) {
        DatePicker dataPicker = new DatePicker(this);
        dataPicker.selectDateDialog(textView, DateUtil.getCurrentDate(DateUtil.dateFormatYMD));
        dataPicker.getTitle().setText(title);
        dataPicker.setChangeDate(new DatePicker.OnChangeDate() {
            @Override
            public void onChangeDate(String date) {
                businessLicenseInfo.setEffectTime(date);
            }
        });
    }

    /**
     * 过期时间
     */
    private void showExpireTime(TextView textView, String title) {
        DatePicker dataPicker = new DatePicker(this);
        dataPicker.selectDateDialog(textView, DateUtil.getCurrentDate(DateUtil.dateFormatYMD));
        dataPicker.getTitle().setText(title);
        dataPicker.setChangeDate(new DatePicker.OnChangeDate() {
            @Override
            public void onChangeDate(String date) {
                businessLicenseInfo.setExpireTime(date);
            }
        });
    }

}
